package com.lanou.myapp.comic.recommendedcomic.contentrecommendedcomic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class ContentRecommendedComicAdapter extends BaseAdapter {
    private List<Picture> datas;
    private Context context;
    private ImageLoader imageLoader;
//    private RequestQueue queue;
//    private LruCache<String, Bitmap> lruCache;
//    private ImageLoader.ImageCache imageCache;
//    private VolleySingleton volleySingleton;


    public ContentRecommendedComicAdapter(Context context) {
        this.context = context;
//        init();
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }


//    private void init() {
//        queue = Volley.newRequestQueue(context);
//
//        lruCache  = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//
//        imageLoader = new ImageLoader(queue, imageCache);
//    }



//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.img_lauch_bitch,
//                R.mipmap.img_lauch_bitch);
//        imageLoader.get(imgUrl, listener, 600, 800);
//    }

    public void addData(List<Picture> data) {
        datas = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size() > 0 && datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            myViewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.content_item_recommended_comic, null);
            myViewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_content_item_recommended_comic);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

//        String url_img = datas.get(position).getF();
//        getBitmapCache(url_img, myViewHolder.imageView);
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
//        VolleySingleton.getImageLoader().get(datas.get(position).getF(), listener,400,400);
        Picasso.with(context).load(datas.get(position).getF()).resize(400,400).into(myViewHolder.imageView);



//        Glide.with(context).load(datas.get(position).getF()).into(myViewHolder.imageView);


//        myViewHolder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//        myViewHolder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//        myViewHolder.networkImageView.setImageUrl(datas.get(position).getF(), imageLoader);


        return convertView;
    }


    class MyViewHolder {
        private ImageView imageView;
        private NetworkImageView networkImageView;
    }

}
